package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private StrategyMapper strategyMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;


    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        // 目的地
        Strategy strategy = strategyMapper.get(strategyDetail.getStrategy().getId());
        strategyDetail.setDest(strategy.getDest());

        // 摘要
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        String content = strategyContent.getContent();
        if (content.length() > 200) {
            strategyDetail.setSummary(content.substring(0, 200));
        } else {
            strategyDetail.setSummary(content);
        }

        // 是否是国外
        List<Destination> toasts = destinationService.queryForParent(strategyDetail.getDest().getId());
        Destination destination = toasts.get(0);
        if (destination.getId() == 1) {
            strategyDetail.setIsabroad(StrategyDetail.ABROAD_NO);
        } else {
            strategyDetail.setIsabroad(StrategyDetail.ABROAD_YES);
        }


        if (strategyDetail.getId() == null) {
            // 保存
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

        } else {
            // 修改
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        // 打破关系
        strategyTagMapper.deleteRelation(strategyDetail.getId());
        if (tags.length() != 0) {
            String[] tagArr = tags.split(",");
            for (String tag : tagArr) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(tag);
                strategyTagMapper.insert(strategyTag);
                strategyTagMapper.insertRelation(strategyTag.getId(), strategyDetail.getId());
            }
        }
    }


    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void changeState(Long id, int state) {
        strategyDetailMapper.updateState(id, state);
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long id) {
        return strategyDetailMapper.selectViewNumTop3(id);
    }

    @Override
    public void updataReplyNum(Long id, int replynum) {
        strategyDetailMapper.updataReplyNum(id, replynum);
    }

    @Override
    public void updateStatis(StrategyPersistenceVO persistenceVO) {
        strategyDetailMapper.updateStatis(persistenceVO);
    }

    @Override
    public StrategyDetail selectStrategyDetailTop1() {
        return strategyDetailMapper.selectStrategyDetailTop1();
    }

    public List<StrategyDetail> selectDetailByUserId(Long id) {
        return strategyDetailMapper.selectDetailByUserId(id);
    }
}
